package com.tianhui.consignortwofei.mvp.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.widget.itemView.DetailItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.pattern.orderStatus.status.IOrderStatus;
import com.tianhui.consignortwofei.role.RoleController;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import java.math.BigDecimal;

/**
 * @author fgs
 * @date 2020/8/27
 * @description
 **/
public class OrderListAdapter extends RecyclerArrayAdapter<OrderInfo> {
    private IOrderStatus iOrderStatus;

    public OrderListAdapter(Context context, IOrderStatus iOrderStatus) {
        super(context);
        this.iOrderStatus = iOrderStatus;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderListViewHolder(parent, R.layout.item_order_list);
    }

    private class OrderListViewHolder extends BaseViewHolder<OrderInfo> {

        private TextView orderNumberTextView;
        private TextView orderStatusTextView;
        private DetailItemView oneDetailItemView;
        private DetailItemView twoDetailItemView;
        private DetailItemView threeDetailItemView;
        private DetailItemView fourDetailItemView;
        private DetailItemView fiveDetailItemView;
        private DetailItemView sixDetailItemView;
        private DetailItemView sevenDetailItemView;
        private DetailItemView eightDetailItemView;
        private DetailItemView timeDetailItemView;
        private LinearLayout freightLinearLayout;
        private TextView freightTextView;
        private TextView submitTextView;
        private TextView shareCodeTextView;
        private TextView editOrderTextView;
        private LinearLayout goodsDetailLinearLayout;
        private TextView goodsWeightTextView;
        private TextView grabWeightTextView;
        private TextView priceTextView;
        private TextView tv_tonghua;
        private TextView tv_liulan;




        public OrderListViewHolder(ViewGroup parent, int res) {
            super(parent, res);
            orderNumberTextView = getView(R.id.item_order_list_topLeftTextView);
            orderStatusTextView = getView(R.id.item_order_list_topRightTextView);

            oneDetailItemView = getView(R.id.item_order_list_oneDetailItemView);
            twoDetailItemView = getView(R.id.item_order_list_twoDetailItemView);
            threeDetailItemView = getView(R.id.item_order_list_threeDetailItemView);
            fourDetailItemView = getView(R.id.item_order_list_fourDetailItemView);
            fiveDetailItemView = getView(R.id.item_order_list_fiveDetailItemView);
            sixDetailItemView = getView(R.id.item_order_list_sixDetailItemView);
            sevenDetailItemView = getView(R.id.item_order_list_sevenDetailItemView);
            eightDetailItemView = getView(R.id.item_order_list_eightDetailItemView);
            timeDetailItemView = getView(R.id.item_order_list_timeDetailItemView);

            freightLinearLayout = getView(R.id.item_order_list_freightLinearLayout);
            freightTextView = getView(R.id.item_order_list_freightTextView);
            submitTextView = getView(R.id.item_order_list_submitTextView);
            shareCodeTextView = getView(R.id.item_order_list_shareOrderCodeTextView);
            editOrderTextView = getView(R.id.item_order_list_editOrderTextView);

            goodsDetailLinearLayout = getView(R.id.item_order_list_goodsDetailLinearLayout);
            goodsWeightTextView = getView(R.id.item_order_list_goodsWeightTextView);
            grabWeightTextView = getView(R.id.item_order_list_grabWeightTextView);
            priceTextView = getView(R.id.item_order_list_priceTextView);

            tv_tonghua = getView(R.id.tv_tonghua);
            tv_liulan = getView(R.id.tv_liulan);

            addOnClickListener(R.id.item_order_list_submitTextView);
            addOnClickListener(R.id.item_order_list_shareOrderCodeTextView);
            addOnClickListener(R.id.item_order_list_editOrderTextView);

            //记录
            addOnClickListener(R.id.tv_tonghua);
            addOnClickListener(R.id.tv_liulan);
        }

        @Override
        public void setData(OrderInfo data) {
            super.setData(data);
            //货源单号
            orderNumberTextView.setText("货源单号：" + data.billno);
            //取单地
            twoDetailItemView.setContent(data.placeprovince + data.placecity + data.placecounty + data.placedetails);
            //装货地
            threeDetailItemView.setContent(data.deliverprovince + data.delivercity + data.delivercounty + data.deliverdetails);
            //目的地
            fourDetailItemView.setContent(data.destinationprovince + data.destinationcity + data.destinationcounty + data.destinationdetails);
            //货权
            if (RoleController.getInstance().isShowCustomerName()) {
                sixDetailItemView.setVisibility(View.VISIBLE);
                sixDetailItemView.setContent(data.customername);
                sixDetailItemView.setTitle("货权：");
            } else {
                sixDetailItemView.setVisibility(View.GONE);
            }
            //车牌号
            if (RoleController.getInstance().isDispatchOrderShowVehicleNumber()) {
                eightDetailItemView.setVisibility(View.VISIBLE);
                eightDetailItemView.setContent(data.vehiclenum);
                eightDetailItemView.setTitle("车牌号：");
            } else {
                eightDetailItemView.setVisibility(View.GONE);
            }

            //发单时间
            timeDetailItemView.setContent(data.sendordertime);
            if (iOrderStatus != null) {
                //状态
                if (!TextUtils.isEmpty(iOrderStatus.getOrderStatus(data))) {
                    orderStatusTextView.setText(Html.fromHtml(iOrderStatus.getOrderStatus(data)));
                    orderStatusTextView.setVisibility(View.VISIBLE);
                } else {
                    orderStatusTextView.setVisibility(View.INVISIBLE);
                }
                //调度单号
                String dispatchNumber = iOrderStatus.getDispatchNumber(data);
                if (!TextUtils.isEmpty(dispatchNumber)) {
                    oneDetailItemView.setVisibility(View.VISIBLE);
                    oneDetailItemView.setContent(iOrderStatus.getDispatchNumber(data));
                } else {
                    oneDetailItemView.setVisibility(View.GONE);
                }
                //运费总计
                String freight = iOrderStatus.getFreight(data);
                freightTextView.setText(freight);
                if (iOrderStatus.getOrderStatusType() == OrderType.TYPE_PAYMENT) {
                    freightLinearLayout.setVisibility(View.VISIBLE);
                    if (!TextUtils.isEmpty(data.issettle) && (data.issettle.equals("0") || data.issettle.equals("3"))) {
                        submitTextView.setVisibility(View.VISIBLE);
                    } else {
                        submitTextView.setVisibility(View.GONE);
                    }

                } else {
                    submitTextView.setVisibility(View.GONE);
                    freightLinearLayout.setVisibility(View.GONE);
                }


                //货源单
                if (iOrderStatus.getOrderStatusType() == OrderType.TYPE_SOURCE
                        ||iOrderStatus.getOrderStatusType() == OrderType.TYPE_SOURCE_DINGJIN) {
                    //分享码
                    shareCodeTextView.setVisibility(View.VISIBLE);
//                    //编辑
//                    editOrderTextView.setVisibility(View.VISIBLE);
                    //货物明细
                    goodsDetailLinearLayout.setVisibility(View.VISIBLE);
                    goodsWeightTextView.setText("总重：" + data.weight + "吨");
                    if (!TextUtils.isEmpty(data.detachable) && data.detachable.equals("1")) {
                        String grabWeight = "0";
                        if (!TextUtils.isEmpty(data.weight) && !TextUtils.isEmpty(data.leftweight)) {
                            grabWeight = new BigDecimal(data.weight).subtract(new BigDecimal(data.leftweight)).toString();
                        }
                        grabWeightTextView.setText("已抢：" + grabWeight + "吨");
                        grabWeightTextView.setVisibility(View.VISIBLE);
                    } else {
                        grabWeightTextView.setVisibility(View.GONE);
                    }
                    priceTextView.setText("单价：" + data.baseprice + "元/吨");

                    //指定承运人
                    if (!TextUtils.isEmpty(data.sentto) && data.sentto.equals("02") && !TextUtils.isEmpty(data.senttodetails)) {
                        fiveDetailItemView.setVisibility(View.VISIBLE);
                        fiveDetailItemView.setTitle("指定承运人：");
                        fiveDetailItemView.setContent(data.senttodetails);
                    } else {
                        fiveDetailItemView.setVisibility(View.GONE);
                    }
                    //询价
                    sevenDetailItemView.setVisibility(View.VISIBLE);
                    sevenDetailItemView.setTitle("是否询价：");
                    if (!TextUtils.isEmpty(data.ishide) && data.ishide.equals("1")) {
                        sevenDetailItemView.setContent("是" + "（" + data.facetel + "）");
                    } else {
                        sevenDetailItemView.setContent("否");
                    }

                } else {
                    editOrderTextView.setVisibility(View.GONE);
                    shareCodeTextView.setVisibility(View.GONE);
                    goodsDetailLinearLayout.setVisibility(View.GONE);
                }

            }

        }
    }
}
